<template>
  <TopNavBar title="订单" type="back" />
  <div class="aiarb-order-page">
    <div class="order-tabs">
      <div :class="['tab', {active: tab === 'hold'}]" @click="selectActive('hold')">持仓明细</div>
      <div :class="['tab', {active: tab === 'history'}]" @click="selectActive('history')">历史明细</div>
    </div>
    <div v-if="orderList.length === 0" class="order-empty">
      <img class="empty-icon" src="/static/img/W2k_c7838bd9_spH.cd0c5744.png" alt="暂无纪录" />
      <div class="empty-text">暂无纪录</div>
    </div>
    <div v-else class="order-list">
      <div class="order-card" v-for="(item, idx) in orderList" :key="idx">
        <div class="order-card-header">
          <span class="order-symbol">{{ item.coinSymbol }}/{{ item.baseSymbol }}</span>
          <span class="order-signal" v-if="item.gameType == 0">看漲</span>
          <span class="order-signal" v-if="item.gameType == 1">看跌</span>
          <span class="order-status" v-if="item.status == 0">持倉中</span>
          <span class="order-status" v-if="item.status == 1">已完成</span>
        </div>
        <div class="order-card-row">
          <span class="order-label">买入时间</span>
          <span class="order-value">{{ item.createTime }}</span>
        </div>
        <div class="order-card-row">
          <div>
            <img class="order-ex-logo" :src="item.buyEx?.logo" :alt="item.buyEx?.name" />
          <span class="order-label">{{ item.buyEx?.name }}价格</span>
          </div>
          <span class="order-value">{{ item.openPrice }} USD</span>
        </div>
        <div class="order-card-row">
          <span class="order-label">代币数量</span>
          <span class="order-value">{{ item.betAmount }}</span>
        </div>
        <!-- <div class="order-card-row">
          <span class="order-label">买入金额</span>
          <span class="order-value">{{ item.buyTotal }} USD</span>
        </div> -->
        <div class="order-card-row">
          <div>
            <img class="order-ex-logo" :src="item.sellEx?.logo" :alt="item.sellEx?.name" />
          <span class="order-label">{{ item.sellEx?.name }}价格</span>
          </div>
          <span class="order-value">{{ item.closePrice }} USD</span>
        </div>
        <div class="order-card-row">
          <span class="order-label">卖出时间</span>
          <span class="order-value">{{ item.closeTime }}</span>
        </div>
        <div class="order-card-row">
          <span class="order-label">获利</span>
          <span class="order-profit" :class="{'profit-up': item.rewardAmount > 0, 'profit-down': item.rewardAmount < 0}">{{ item.rewardAmount > 0 ? '+' : '' }}{{ item.rewardAmount }} USD</span>
        </div>
        <div class="order-card-row">
          <span class="order-label">手续费</span>
          <span class="order-value">{{ item.rate * item.betAmount }} USD</span>
        </div>
        <button class="order-detail-btn" @click="goDetail(item)">看详情</button>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { secondContractOrderselectOrderList } from '@/api/trade/index'
const tab = ref('hold')
const router = useRouter()
// 静态订单数据示例
const orderList = ref([
  // {
  //   id: '2025031200205106460000879489774',
  //   symbol: 'BTC/USDT',
  //   signal: '↑看涨',
  //   status: '已完成',
  //   buyTime: '2025-03-12 00:20:51',
  //   buyEx: {
  //     name: 'Crypto',
  //     logo: 'https://xinyuanimg-c.oss-cn-hongkong.aliyuncs.com/public/949bc8d1f5d1491d8f2d57e07cf553e2.png',
  //   },
  //   buyPrice: '81,034.82',
  //   amount: '0.001234',
  //   buyTotal: '100',
  //   sellEx: {
  //     name: 'Binance',
  //     logo: 'https://xinyuanimg-c.oss-cn-hongkong.aliyuncs.com/public/bc2525ef9de3407f970043b98df9e2d5.png',
  //   },
  //   sellPrice: '80,694.463436',
  //   sellTime: '2025-03-12 00:21:15',
  //   profit: -0.29,
  //   fee: 0,
  // }
])
function selectActive(val) {
  tab.value = val
  if (val == 'hold') {
    getList(0)
  } else {
    getList(1)
  }
  
}
const getList = async (status) => {
  try {
    let res = ''
    if (status == 0) {
      res = await secondContractOrderselectOrderList({ status: status, aiType: '3' })
    } else {
      res = await secondContractOrderselectOrderList({ aiType: '3' })
    }
    
    if (res.code === 200) {
      const result = res.data
      orderList.value = res.data
      // historyList.value = fifterTimeDown(result)
      // historyNewList.value = historyList.value
      // filterEyes()
      // closeToast()
    } else {
      // closeToast()
    }
  } catch (error) {
    // closeToast()
  }
}

getList(0)

function goDetail(item) {
  router.push({
    path: '/aiArbOrderDetail',
    query: { id: item.id }
  })
}
</script>

<style scoped>
.aiarb-order-page {
  background: #f7f8fa;
  min-height: 100vh;
  padding: 0;
}
.order-tabs {
  display: flex;
  justify-content: flex-start;
  background: #fff;
  border-radius: 16px;
  margin: 16px 12px 0 12px;
  overflow: hidden;
  padding: 4px;
}
.tab {
  flex: 1;
  text-align: center;
  padding: 12px 0;
  font-size: 16px;
  color: #222;
  cursor: pointer;
  font-weight: 600;
  border-radius: 12px;
  background: transparent;
  transition: color 0.2s, background 0.2s;
}
.tab.active {
  color: #fff;
  background: #000;
}
.order-empty {
  display: flex;
  flex-direction: column;
  align-items: center;
  margin-top: 60px;
}
.empty-icon {
  width: 120px;
  height: 120px;
  margin-bottom: 16px;
}
.empty-text {
  color: #888;
  font-size: 18px;
  font-weight: 600;
}
.order-list {
  margin: 24px 12px 0 12px;
}
.order-card {
  background: #fff;
  border-radius: 18px;
  box-shadow: 0 2px 8px 0 rgba(0,0,0,0.04);
  padding: 20px 18px 18px 18px;
  margin-bottom: 18px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.order-card-header {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 8px;
}
.order-symbol {
  font-size: 18px;
  font-weight: 700;
  color: #222;
}
.order-signal {
  font-size: 13px;
  color: #00c693;
  background: #e6faf6;
  border-radius: 8px;
  padding: 2px 8px;
  margin-left: 6px;
  font-weight: 600;
}
.order-status {
  margin-left: auto;
  font-size: 15px;
  color: #00c693;
  font-weight: 600;
}
.order-card-row {
  display: flex;
  align-items: center;
  font-size: 14px;
  margin-bottom: 2px;
  justify-content: space-between;
}
.order-label {
  color: #222;
  min-width: 80px;
}
.order-value {
  color: #222;
  margin-left: 8px;
  font-weight: 500;
}
.order-ex-logo {
  width: 20px;
  height: 20px;
  border-radius: 50%;
  margin-right: 6px;
  background: #f6f8fa;
  border: 1px solid #eee;
}
.order-profit {
  margin-left: 8px;
  font-weight: 600;
}
.profit-up {
  color: #00c693;
}
.profit-down {
  color: #00c693;
}
.order-detail-btn {
  margin: 18px auto 0 auto;
  display: block;
  width: 80%;
  background: #000;
  color: #fff;
  font-size: 16px;
  font-weight: 600;
  border: none;
  border-radius: 12px;
  padding: 10px 0;
  cursor: pointer;
  transition: background 0.2s;
}
.order-detail-btn:active {
  background: #222;
}
</style> 